<template>
	<view>
		<simple-toolbar :title="$t('user.resetPwd')">
			<view class="returnToConfirm">
				<el-button @click="navBack">{{ $t('user.return') }}</el-button>
				<el-button @click="confirm" type="primary" :disabled="success">{{ $t('user.confirm') }}</el-button>
			</view>
		</simple-toolbar>

		<view class="pad-base form">
			<el-form :model="formData" :rules="rules" ref="formData" label-position="top">
				<el-form-item :label="$t('user.newPassword')" prop="new_password">
					<el-input v-model="formData.new_password" type="password" :placeholder="$t('user.newPassword_pla')">
					</el-input>
				</el-form-item>
				<el-form-item :label="$t('user.confirmPassword')" prop="new_password2">
					<el-input v-model="formData.new_password2" type="password"
						:placeholder="$t('user.confirmPassword_pla')"></el-input>
				</el-form-item>
			</el-form>
		</view>
	</view>
</template>

<script>
	import {
		changePwd,
		resetPwd
	} from "../../js_sdk/uni-admin/user.js"
	import {
		mapMutations
	} from "vuex"

	export default {
		onLoad: function(option) {
			let item = option.token
			this.formData.token = item
		},
		data() {
			return {
				success: false,
				formData: {
					new_password: '',
					new_password2: ''
				},
				rules: {
					new_password: [{
						required: true,
						// message: this.$t('user.newPassword_pla'),
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('user.newPassword_pla')))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
					new_password2: [{
						required: true,
						// message: this.$t('user.confirmPassword_pla'),
						validator: (rule, value, callback) => {
							if (value === '') {
								callback(new Error(this.$t('user.confirmPassword_pla')))
							} else if(this.formData.new_password !== value) {
								callback(new Error('两次输入的密码不一致!'))
							} else {
								callback()
							}
						},
						trigger: 'blur'
					}],
				}
			}
		},
		methods: {
			navBack() {
				uni.navigateBack()
			},
			confirm() {
				this.$refs.formData.validate(async (valid) => {
					if (!valid) return
					this.success = true
					await resetPwd(this.formData)
					this.$message.success(this.$t('user.succResetPwd'))
					uni.reLaunch({
						url: "./login"
					})
				})
			}
		},
	}
</script>

<style>
</style>
